import axios from 'axios';
import redux from '../Redux/Redux';

const instance = axios.create({
    baseURL: 'http://meeting.aiolia.top:8080/',
});
instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

redux.subscribe(() => {
    instance.defaults.headers.common['Authorization'] = store.getState().authToken;
});

function convertParamsToData(param) {
    const paramArr = [];
    for (const key in param) {
        if (Object.hasOwnProperty.call(param, key)) {
            const value = param[key];
            paramArr.push(`${encodeURI(key)}=${encodeURI(value)}`);
        }
    }
    return paramArr.length > 0 ? paramArr.join('&') : '';
}

const $ajax = {
    post: function (url, params, headers) {
        return new Promise((resolve, reject) => {
            instance({
                method: 'post',
                url,
                data: convertParamsToData(params),
                headers,
            })
                .then((response) => {
                    resolve(response.data);
                })
                .catch((error) => {
                    reject({
                        error,
                        ajax: true,
                    });
                });
        });
    },
    file: function (url, params, headers = {}) {
        const param = new FormData();
        for (const key in params) {
            if (Object.hasOwnProperty.call(params, key)) {
                param.append(key, params[key]);
            }
        }
        return new Promise((resolve, reject) => {
            instance({
                method: 'post',
                url,
                data: param,
                headers: Object.assign(headers, { 'Content-Type': 'multipart/form-data' }),
            })
                .then((response) => {
                    resolve(response.data);
                })
                .catch((error) => {
                    reject({
                        error,
                        ajax: true,
                    });
                });
        });
    },
    get: function (url, params, headers) { },
};

export default $ajax;
